﻿/*************************************************************************
 * Created by OhZee Creative, LLC
 * 2013
 * This code is licensed under Microsoft Public License (Ms-PL)
 * http://opensource.org/licenses/ms-pl
 * http://ozframework.codeplex.com/
 *************************************************************************/

namespace OhZeeCreative
{
    using System;
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;

    public class ObservableQueue<T> : ObservableCollection<T>
    {
        public ObservableQueue()
        {
        }

        public ObservableQueue(int length)
        {
            this.Length = length;
        }

        private int length = int.MaxValue;
        public int Length
        {
            get { return length; }
            set
            {
                if (value < 1)
                {
                    throw new ArgumentOutOfRangeException();
                }
                else if (value != length)
                {
                    while (value < this.Items.Count)
                    {
                        this.Dequeue();
                    }
                    length = value;
                }
            }
        }

        public T Dequeue()
        {
            T item = Items[0];
            RemoveItem(0);
            return item;
        }

        public void Enqueue(T item)
        {
            if (Count == Length)
            {
                Dequeue();
            }
            InsertItem(Count, item);
        }
    }
}
